package org.itboys.mobile.controller.small;

import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;
import org.itboys.commons.CommonConstants;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.mobile.service.mongo.weixin.SundayWeiXinCommentService;
import org.itboys.mobile.util.ResponseMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author：贾琦磊
 * @Date：2018/7/16
 * @Description：
 */
@Controller
@RequestMapping("/sunday/small/comment")
@Api(value = "comment",description = "评论")
public class SundaySmallCommentController extends BaseController{
    @Autowired
    private SundayWeiXinCommentService commentService;

    @ApiOperation("用户点评楼盘/修改点评")
    @RequestMapping(value = "/save",method = RequestMethod.POST)
    public void save(HttpServletRequest request, HttpServletResponse response
            , @ApiParam(value = "用户id",required = true)@RequestParam(value = "memberId",required = true)Long memberId
            , @ApiParam(value = "楼盘id",required = true)@RequestParam(value = "productId",required = true)Long productId
            , @ApiParam(value = "评分",required = true)@RequestParam(value = "rank",required = true)Integer rank
            , @ApiParam(value = "用户身份",required = true)@RequestParam(value = "identity",required = true)String identity
            , @ApiParam(value = "内容",required = true)@RequestParam(value = "content",required = true)String content){
        try {
            commentService.save(memberId,productId,rank,identity,content);
            ResponseMessage.success();
        } catch (Exception e) {
            String msg = CommonConstants.ERROMSG;
            Integer code = CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if (e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")) {
                msg = e.getMessage();
                //token已过期
            } else if (e.getClass().getSimpleName().equalsIgnoreCase("TokenException")) {
                code = CommonConstants.TOKEN_CODE;
                msg = CommonConstants.TOKENMSG;
            } else {
                logger.error("/sunday/small/comment/save----e=" + e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(code, msg);
        }
    }

    @ApiOperation("根据楼盘id获取楼盘点评或者根据用户id获取【我的点评】，用户id和楼盘id不可多传")
    @RequestMapping(value = "/select",method = RequestMethod.POST)
    public void select(HttpServletRequest request,HttpServletResponse response
    ,@ApiParam(value = "用户id",required = false)@RequestParam(value = "memberId",required = false)Long memberId
     ,@ApiParam(value = "楼盘Id",required = false)@RequestParam(value = "productId",required = false)Long productId
     ,@ApiParam(value = "起始页",required = true) @RequestParam(value = "pageNumber",required = true) String pageNumber
     ,@ApiParam(value = "每页大小",required = true) @RequestParam(value = "pageSize",required = true)String pageSize){
        try {

            ResponseMessage.success( commentService.select(request,memberId,productId));
        } catch (Exception e) {
            String msg = CommonConstants.ERROMSG;
            Integer code = CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if (e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")) {
                msg = e.getMessage();
                //token已过期
            } else if (e.getClass().getSimpleName().equalsIgnoreCase("TokenException")) {
                code = CommonConstants.TOKEN_CODE;
                msg = CommonConstants.TOKENMSG;
            } else {
                logger.error("/sunday/small/comment/select----e=" + e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(code, msg);
        }
    }
}
